package polygon.cli;

import java.util.Properties;
import java.util.Map;
import java.io.IOException;

/**
 * @author MikeMirzayanov (mirzayanovmr@gmail.com)
 */
public class Layout {
    private static Properties properties = new Properties();

    public static String getChecker(Map<String, String> params) {
        return process(properties.getProperty("layout.checker"), params);
    }

    public static String getSolution(Map<String, String> params) {
        return process(properties.getProperty("layout.solution"), params);
    }

    public static String getProblemStatements(Map<String, String> params) {
        return process(properties.getProperty("layout.problem.statements"), params);
    }

    public static String getContestStatements(Map<String, String> params) {
        return process(properties.getProperty("layout.contest.statements"), params);
    }

    public static String getTests(Map<String, String> params) {
        return process(properties.getProperty("layout.tests"), params);
    }

    private static String process(String value, Map<String, String> params) {
        String[] tokens = value.split("\\{");
        StringBuilder builder = new StringBuilder();

        for (String token : tokens) {
            int pos = token.indexOf('}');
            if (pos >= 0) {
                String key = token.substring(0, pos);
                if (params.containsKey(key)) {
                    builder.append(params.get(key)).append(token.substring(pos + 1));
                } else {
                    builder.append(token);
                }
            } else {
                builder.append(token);
            }
        }

        return builder.toString();
    }

    static {
        try {
            properties.load(Layout.class.getResourceAsStream("/layout.properties"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
